/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.utils.SystemConfigUtils;
import com.intelligent.ispc.client.utils.UIConstant;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.core.dto.GrrSourceDto;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by cherry on 2016/8/29.
 */
public class SourceTableModel extends AbstractTableModel {
    private String[] columns;
    private List<GrrSourceDto> data;

    private int digGrrNum = UIConstant.DEFAULT_GRR_DIGNUM;
    private int digPercent = UIConstant.DEFAULT_DIGNUM_PERCENT;

    @Override
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getColumnCount() {
        return columns == null ? 0 : columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return (columns == null) || (columns.length <= column) ? "" : columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data == null) {
            return "";
        }
        GrrSourceDto grrSourceDto = data.get(rowIndex);
        String keyValue = grrSourceDto.getKeyValue();
        String variation = grrSourceDto.getVariation();
        String sigma = grrSourceDto.getSigma();
        String xSigma = grrSourceDto.getxSigma();
        String contribution = grrSourceDto.getContribution();
        String totalVariation = grrSourceDto.getTotalVariation();
        String tolerance = grrSourceDto.getTolerance();

        switch (columnIndex) {
            case 0:
                return StringUtils.isBlank(keyValue) ? "-" : keyValue;
            case 1:
                return StringUtils.isNumeric(variation) ? StringUtils.formatDouble(Double.valueOf(variation), digGrrNum) : "-";
            case 2:
                return StringUtils.isNumeric(sigma) ? StringUtils.formatDouble(Double.valueOf(sigma), digGrrNum) : "-";
            case 3:
                return StringUtils.isNumeric(xSigma) ? StringUtils.formatDouble(Double.valueOf(xSigma), digGrrNum) : "-";
            case 4:
                return StringUtils.isNumeric(contribution) ? StringUtils.formatDouble(Double.valueOf(contribution), digPercent) + "%" : "-";
            case 5:
                return StringUtils.isNumeric(totalVariation) ? StringUtils.formatDouble(Double.valueOf(totalVariation), digPercent) + "%" : "-";
            case 6:
                return StringUtils.isNumeric(tolerance) ? StringUtils.formatDouble(Double.valueOf(tolerance), digPercent) + "%" : "-";
            default:
                return "";
        }
    }

    /**
     * SourceTableModel constructor
     */
    public SourceTableModel() {

    }

    /**
     * SourceTableModel constructor
     *
     * @param columns SourceTable columns
     */
    public SourceTableModel(String[] columns) {
        if (columns != null) {
            this.columns = new String[columns.length];
            System.arraycopy(columns, 0, this.columns, 0, columns.length);
        }
        data = Lists.newArrayList();
    }

    /**
     * set SourceTableModel data source
     *
     * @param dtos SourceTableModel data source
     */
    public void setData(List<GrrSourceDto> dtos) {
        if (dtos == null) {
            data = Lists.newArrayList();
        } else {
            data.clear();
            data.addAll(dtos);
        }
    }

    public List<GrrSourceDto> getData() {
        return data;
    }

    /**
     * init digNum value
     */
    public void initDigNum() {
        digGrrNum = SystemConfigUtils.getInstance().getGrrDigit();
        digPercent = (digGrrNum >= 2) ? (digGrrNum - 2) : 0;
    }
}
